<script setup lang="ts">
import { getUser } from "@/api/search";
import { update_user } from "@/api/user";
import { ElImage, type UploadProps } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
const router = useRouter();
interface Props {
  show: boolean;
  id: string;
  imgUrl: string;
  isTable: boolean;
}
const props = withDefaults(defineProps<Props>(), {
  show: false,
  isTable: false,
  id: "",
  imgUrl: "",
});

const imageUrl = ref(props.imgUrl);
const fileList = ref([]);

const handleChange = (uploadFile: any, uploadFiles: any) => {
  // console.log(uploadFile, uploadFiles);
  fileList.value = uploadFiles;
  imageUrl.value = URL.createObjectURL(uploadFile.raw);
  console.log(imageUrl.value);
  
};
const emits = defineEmits(["close", "submit"]);
/**
 * 关闭
 */
const closed = () => {
  // console.log("11111111");

  emits("close", false);
};
const userData: any = ref({});
const getLogData = async (id: any) => {
  let res: any = await getUser({
    pageSize: 999,
    pageNum: 1,
    id,
  });
  if (res.code == 0) {
    userData.value = res.data.records[0];
    // console.log(userData.value);
    imageUrl.value = import.meta.env.VITE_APP_API_URL + userData.value?.avatar + '?'+Math.random();
  }
};
const user_info: any = JSON.parse(localStorage.getItem("userData"));

const val = computed(() => props.show);
// const val = ref(true);
watch(
  val,
  (val) => {
    console.log("val", val, props.id);
    if (val && props.id) {
      getLogData(props.id);
    }
  },
  { immediate: true }
);
const options = [
  {
    value: "0",
    label: "管理员",
  },
  {
    value: "1",
    label: "普通用户",
  },
];
const submit = async () => {
  let params = {
    ...userData.value,
    avatar: fileList.value[0]?.raw || '',
  };
  let formdata = new FormData();
  Object.keys(params).forEach((key) => {
    formdata.append(key, params[key]);
  });
  // console.log("params", params);
  let res = await update_user(formdata);
  // emits("submit", params);
  if (res.code == 0) {
    ElMessage({
      type: "success",
      message: res?.msg,
    });
    if (!props.isTable) {
      localStorage.setItem("userData", JSON.stringify(res.data));
    }
    closed();
    router.go(0)
  }
};
</script>

<template>
  <!-- <div class="user-edit"> -->
  <el-dialog
    v-model="val"
    title="用户信息"
    width="500"
    destroy-on-close
    center
    @close="closed"
    draggable
    append-to-body
  >
    <el-form :model="userData" label-width="auto" style="max-width: 600px">
      <el-form-item label="昵称">
        <el-input v-model="userData.nickname" />
      </el-form-item>
      <el-form-item label="账号" v-if="user_info?.permissions == '0'">
        <el-input v-model="userData.username" />
      </el-form-item>
      <el-form-item label="密码" v-if="user_info?.id == id">
        <el-input type="password" v-model="userData.password" show-password />
      </el-form-item>
      <el-form-item v-if="user_info?.permissions == '0'" label="权限">
        <el-select
          v-model="userData.permissions"
          placeholder="Select"
          style="width: 240px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="头像">
        <div class="avatar">
          <el-image
            @click.stop=""
            @drop.stop=""
            fit="fill"
            :preview-src-list="[imageUrl]"
            v-if="imageUrl"
            :src="imageUrl"
            class="avatar"
            style="width: 178px; height: 178px"
          />
          <el-upload
            v-model:file-list="fileList"
            action="#"
            class="avatar-uploader"
            :auto-upload="false"
            :show-file-list="false"
            :on-change="handleChange"
            limit="1"
            accept=".jpg,.png"
          >
            <el-icon class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closed">取消</el-button>
        <el-button type="primary" @click="submit"> 确认修改 </el-button>
      </div>
    </template>
  </el-dialog>
  <!-- </div> -->
</template>

<style lang="scss" scoped>
// .user-edit {
:deep(.avatar-uploader .avatar) {
  width: 178px;
  height: 178px;
  display: block;
}
:deep(.avatar-uploader .el-upload) {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

:deep(.avatar-uploader .el-upload:hover) {
  border-color: var(--el-color-primary);
}

:deep(.el-icon.avatar-uploader-icon) {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
.avatar {
  display: flex;
  :deep(.el-image) {
    width: 178px;
    height: 178px;
    > img {
      width: 100%;
    }
  }
}
// }
</style>
